import 'dart:async';
import 'dart:io';
import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/widgets.dart';
import 'package:gameplay_flutter/lib_baseview/lib_baseview.dart';
import 'package:gameplay_flutter/lib_runtime/common/lanuage_util.dart';
import '../utils/constant/text_styles.dart';
import 'color/jr_base_color.dart';
import 'color/jr_dark_color.dart';
import 'color/jr_light_color.dart';

/// 资源管理类
class JR {
  JR._();

  static ThemeStyle themeStyle = ThemeStyle.light;

  static late JRBaseColor color;

  static late JRTextStyles textStyle;

  static Brightness brightness =
      WidgetsBinding.instance.window.platformBrightness;

  static StreamController<ThemeStyle>? _themeStyleStream;

  static void init({ThemeStyle themeStyle = ThemeStyle.light}) {
    JR.themeStyle = themeStyle;
    color = JR.themeStyle.color;
    brightness = JR.themeStyle.brightness;
    _themeStyleStream ??= StreamController.broadcast();
  }

  static Stream<ThemeStyle>? get subscribeThemeStyle =>
      _themeStyleStream?.stream;

  static void update(ThemeStyle themeStyle) {
    init(themeStyle: themeStyle);
    _themeStyleStream?.add(themeStyle);
  }

  static ThemeStyle get light => ThemeStyle.light;
  static ThemeStyle get dark => ThemeStyle.dark;

  /// 获取图片路径
  ///
  /// [package]为null时正常取图片，不为空时优先取模块内部的资源，但是App内assets/module/$package下的相同子路径存在时，优先从App取图
  static String imagePath(String path, {String? package}) {
    if (path.startsWith('assets/images') ||
        (path.startsWith('packages/') && path.contains('assets/images'))) {
      return path;
    }
    if (package != null && package.isNotEmpty) {
      return 'packages/$package/assets/images/$path';
    } else {
      return 'assets/images/$path';
    }
  }

  /// path 相对路径
  static Widget img(
    String path, {
    Key? key,
    String? package,
    double? width,
    double? height,
    Color? color,
    BoxFit? fit,
    int? cachedWidth,
    int? cachedHeight,
    bool matchTextDirection = false,
    bool isFile = false,
    Rect? centerSlice,
    bool disableCached = false,
    double scale = 1.0,
  }) {
    if (path.isEmpty) {
      return SizedBox(
        width: width,
        height: height,
      );
    }
    if (path.startsWith('http')) {
      // if (path.endsWith('.pag')) {
      //   return PAGView.network(path,
      //       width: width, repeatCount: -1, height: height, autoPlay: true);
      // }
      return CachedNetworkImage(
        imageUrl: path,
        width: width,
        height: height,
        fit: fit,
        matchTextDirection: matchTextDirection,
      );
    }
    String imgPath = imagePath(path, package: package);

    if (path.endsWith('.svg')) {
      return Container();

      // return isFile
      //     ? SvgPicture.file(
      //         File(path),
      //         width: width,
      //         height: height,
      //         color: color,
      //         fit: fit ?? BoxFit.fill,
      //         key: key,
      //         matchTextDirection: matchTextDirection,
      //       )
      //     : SvgPicture.asset(
      //         imgPath,
      //         width: width,
      //         height: height,
      //         color: color,
      //         fit: fit ?? BoxFit.fill,
      //         key: key,
      //         matchTextDirection: matchTextDirection ?? false,
      //       );
    } else if (path.endsWith('.webp')) {
      return RepaintBoundary(
        child: isFile
            ? Image.file(File(path),
                width: width,
                height: height,
                color: color,
                fit: fit,
                key: key,
                scale: scale,
                centerSlice: centerSlice,
                cacheWidth: cachedWidth,
                cacheHeight: cachedHeight,
                matchTextDirection: matchTextDirection)
            : LanguageUtil().isRTL()
                ? Transform.scale(
                    scaleX: -1, // 水平翻转
                    child: Image.asset(imgPath,
                        width: width,
                        height: height,
                        color: color,
                        fit: fit,
                        key: key,
                        scale: scale,
                        centerSlice: centerSlice,
                        cacheWidth: cachedWidth,
                        cacheHeight: cachedHeight,
                        matchTextDirection: matchTextDirection))
                : Image.asset(imgPath,
                    width: width,
                    height: height,
                    color: color,
                    fit: fit,
                    key: key,
                    scale: scale,
                    centerSlice: centerSlice,
                    cacheWidth: cachedWidth,
                    cacheHeight: cachedHeight,
                    matchTextDirection: matchTextDirection),
      );
    } else {
      return isFile
          ? Image.file(File(path),
              width: width,
              height: height,
              color: color,
              fit: fit,
              key: key,
              scale: scale,
              cacheWidth: cachedWidth,
              centerSlice: centerSlice,
              cacheHeight: cachedHeight,
              matchTextDirection: matchTextDirection)
          : LanguageUtil().isRTL()
              ? Transform.scale(
                  scaleX: -1, // 水平翻转
                  child: Image.asset(imgPath,
                      width: width,
                      height: height,
                      color: color,
                      fit: fit,
                      key: key,
                      scale: scale,
                      cacheWidth: cachedWidth,
                      centerSlice: centerSlice,
                      cacheHeight: cachedHeight,
                      matchTextDirection: matchTextDirection))
              : Image.asset(imgPath,
                  width: width,
                  height: height,
                  color: color,
                  fit: fit,
                  key: key,
                  scale: scale,
                  cacheWidth: cachedWidth,
                  centerSlice: centerSlice,
                  cacheHeight: cachedHeight,
                  matchTextDirection: matchTextDirection);
    }
  }
}

enum ThemeStyle { light, dark }

extension ThemeStyleExt on ThemeStyle {
  JRBaseColor get color {
    switch (this) {
      case ThemeStyle.light:
        return JRLightColor();
      case ThemeStyle.dark:
        return JRDarkColor();
      default:
        return JRLightColor();
    }
  }

  Brightness get brightness {
    switch (this) {
      case ThemeStyle.light:
        return Brightness.dark;
      case ThemeStyle.dark:
        return Brightness.light;
      default:
        return Brightness.dark;
    }
  }
}
